const url = '127.0.0.1:3000';
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; // 打包分析
const CompressionWebpackPlugin = require('compression-webpack-plugin') // gzip
const productionGzipExtensions = ['js', 'css', 'html'] // 需要gzip压缩的文件后缀
const path = require('path');
const os = require('os');


function resolve(dir) {
  return path.join(__dirname, dir);
}
console.log('NODE_ENV: ', process.env.NODE_ENV);
console.log('VUE_APP_API:', process.env.VUE_APP_API, process.env.VUE_APP_TWO)
module.exports = {
  publicPath: '/', // 公共路径
  outputDir: 'dist',
  pages: {
    index: {
      entry: 'src/main.ts',
      template: 'public/index.html',
      filename: 'index.html',
      title: 'Index Page',
    },
  },
  css: {
    loaderOptions: {
      less: {
        // lessOptions: {
            javascriptEnabled: true,
        // }
        // data: `@import "~@/styles/variables.less";`
     }
    },
    sourceMap: false,
  },
  runtimeCompiler: false,
  lintOnSave: true, // 开启eslint,在生产构建时禁用 eslint-loader
  //  chainWebpack: (config) => {},
  configureWebpack: (config) => {
    // 支持字符串模板编译
    config.resolve = {
      extensions: ['.js', '.ts', '.tsx', '.vue', '.json', '.css', '.less'],
      alias: {
        '@': resolve('src'),
      },
      mainFields: ['main'],
    };
    config.plugins.push(...[
      new HardSourceWebpackPlugin(), // 开启缓存，进行二次构建优化
    ]);
   
    if (process.env.NODE_ENV === 'production') {
      // 打包分析
      config.plugins.push(
        new BundleAnalyzerPlugin({
          analyzerMode: "static"
        }),
      new CompressionWebpackPlugin({
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8
      })
      ) 
      // 关闭 webpack 的性能提示
      return {
        performance: {
          hints: false,
        },
      };
    }
  },
  parallel: os.cpus().length > 1,
  // pwa: {},
  devServer: {
    proxy: {
      '/api': {
        target: url,
        ws: true,
        pathRewrite: {
          '^/api': '/',
        },
      },
    },
    overlay: {
      warnings: true,
      errors: true,
    },
  },
  productionSourceMap: process.env.NODE_ENV !=='production',
};
